<script type="text/javascript">
$(document).on("click", ".product-edit-link", function () {
	var row = $(this).parent().parent();
	
	var productId = $(this).data('id');
	var name = row.find(".name").text();
	var description = row.find(".description").text();
	var netto_price = row.find(".netto_price").text();
	var price = row.find(".price").text();
	var profit_key = row.find(".profit_key").text();
	var image = row.find(".image").text();
	var category = row.find(".category").text();
	
	populateEditField(productId, name, description, netto_price, price, profit_key, image, category);
});

function populateEditField(productId, name, description, netto_price, price, profit_key, image, category) {
	$("#editProductModal input[name='name']").val(name);
	$("#editProductModal textarea[name='description']").val(description);
	$("#editProductModal input[name='netto_price']").val(netto_price);
	$("#editProductModal input[name='price']").val(price);
	$("#editProductModal input[name='profit_key']").val(profit_key);
	$("#editProductModal input[name='id']").val(productId);
	$("#editProductModal select.category").val(category);
	$("#editProductModal select option[data-name='" + category + "']").attr('selected', 'selected');
}

$("#addProductModal #netto_price").change(function() {
  var price = parseInt($("#addProductModal #netto_price").val());
  var price = Math.round(price * 1.27);
  
  $("#addProductModal #productPrice").val(price);
});

$("#addProductModal #productPrice").change(function() {
  var price = parseInt($("#addProductModal #productPrice").val());
  var price = Math.round(price / 1.27);
  
  $("#addProductModal #netto_price").val(price);
});
</script>